package cn.ibizlab.odoo.core.odoo_mrp.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imrp_routing;
import cn.ibizlab.odoo.core.odoo_mrp.domain.Mrp_routing;
import cn.ibizlab.odoo.core.odoo_mrp.filter.Mrp_routingSearchContext;
import cn.ibizlab.odoo.core.odoo_mrp.service.IMrp_routingService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[工艺] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mrp_routingServiceImpl implements IMrp_routingService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imrp_routingClientService mrp_routingClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imrp_routing clientModel = mrp_routingClientService.createModel();
        clientModel.setId(id);
        mrp_routingClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Mrp_routing get(Integer id) {
        Imrp_routing clientModel = mrp_routingClientService.createModel();
        clientModel.setId(id);
        mrp_routingClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mrp_routing get(Mrp_routing mrp_routing) {
        Mrp_routing retmrp_routing = this.get(mrp_routing.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmrp_routing.getClass(), mrp_routing.getClass(), false);
        copier.copy(retmrp_routing, mrp_routing, null);
        return mrp_routing ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mrp_routingClientService.removeBatch(mrp_routings) ;

    }

    @Override
    public void createBatch(List<Mrp_routing> mrp_routings, int batchSize) {

                        //mrp_routingClientService.createBatch(mrp_routings,batchSize) ;

    }

    @Override
    public boolean create(Mrp_routing mrp_routing) {
        boolean bOk = false ;
        Imrp_routing clientModel = this.convert2Model(mrp_routing,null);
        mrp_routingClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mrp_routing);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Mrp_routing mrp_routing) {
        boolean bOk = false ;
        Imrp_routing clientModel = this.convert2Model(mrp_routing,null);
        mrp_routingClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mrp_routing) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Mrp_routing> mrp_routings, int batchSize) {

        //mrp_routingClientService.updateBatch(mrp_routings,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mrp_routing> searchDefault(Mrp_routingSearchContext context) {
        List<Mrp_routing> list = new ArrayList<Mrp_routing>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing> clientModelList = mrp_routingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据公司查询
     */
    @Override
    public Page<Mrp_routing> selectByOdoo_company(Integer id){
        Mrp_routingSearchContext context = new Mrp_routingSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_routing> list = new ArrayList<Mrp_routing>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing> clientModelList = mrp_routingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_routing> selectByOdoo_create(Integer id){
        Mrp_routingSearchContext context = new Mrp_routingSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_routing> list = new ArrayList<Mrp_routing>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing> clientModelList = mrp_routingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_routing> selectByOdoo_write(Integer id){
        Mrp_routingSearchContext context = new Mrp_routingSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_routing> list = new ArrayList<Mrp_routing>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing> clientModelList = mrp_routingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存位置查询
     */
    @Override
    public Page<Mrp_routing> selectByOdoo_location(Integer id){
        Mrp_routingSearchContext context = new Mrp_routingSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_routing> list = new ArrayList<Mrp_routing>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing> clientModelList = mrp_routingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imrp_routing convert2Model(Mrp_routing domain , Imrp_routing model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mrp_routingClientService.createModel() ;
        }

         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getCodeDirtyFlag())
            model.setCode(domain.getCode());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getOperation_idsDirtyFlag())
            model.setOperation_ids(domain.getOperation_ids());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getLocation_id_textDirtyFlag())
            model.setLocation_id_text(domain.getLocation_id_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getLocation_idDirtyFlag())
            model.setLocation_id(domain.getLocation_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Mrp_routing convert2Domain( Imrp_routing model ,Mrp_routing domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mrp_routing();
        }

        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getCodeDirtyFlag())
            domain.setCode(model.getCode());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getOperation_idsDirtyFlag())
            domain.setOperation_ids(model.getOperation_ids());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getLocation_id_textDirtyFlag())
            domain.setLocation_id_text(model.getLocation_id_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getLocation_idDirtyFlag())
            domain.setLocation_id(model.getLocation_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
